package com.yueyang.sojourn.server.controller.banner;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.dto.AddBannerDto;
import com.yueyang.sojourn.server.model.dto.EditBannerDto;
import com.yueyang.sojourn.server.model.vo.SojournBannerVo;
import com.yueyang.sojourn.server.service.sojourn.banner.ReadBannerService;
import com.yueyang.sojourn.server.service.sojourn.banner.WriteBannerService;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-26
 */
@RestController
@Api(tags = "Banner API")
@RequestMapping("/sojourn/banner")
public class SojournBannerController extends BaseController {

    @Autowired
    private WriteBannerService writeBannerService;
    @Autowired
    private ReadBannerService readBannerService;

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation("新增Banner")
    @PostMapping("/add")
    public void addBanner(@RequestBody @Validated AddBannerDto addBannerDto, BindingResult bindingResult) {
        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();

        writeBannerService.add(loginUserId, addBannerDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation("编辑Banner")
    @PostMapping("/edit")
    public void editBanner(@RequestBody @Validated EditBannerDto editBannerDto, BindingResult bindingResult) {
        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();

        writeBannerService.edit(loginUserId, editBannerDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation("删除Banner")
    @PostMapping("/delete/{bannerId}")
    public void deleteBanner(@PathVariable long bannerId) {
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        writeBannerService.delete(loginUserId, bannerId);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "判断该时间段是否有空余展位", notes = "true代表有空余展位，反之则无")
    @GetMapping("/position/available")
    public boolean positionAvailable(@ApiParam(value = "展示时间开始,格式：yyyy-MM-dd HH:mm:ss", example = "2020-02-20 00:00:00", type = "string") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date showTimeBegin,
                                     @ApiParam(value = "展示时间结束,格式：yyyy-MM-dd HH:mm:ss", example = "2020-03-02 00:00:00", type = "string") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date showTimeEnd,
                                     @ApiParam(value = "排除的bannerId") @RequestParam(value = "excludeBannerId", required = false) Long excludeBannerId) {
        return readBannerService.positionAvailable(showTimeBegin, showTimeEnd, Optional.ofNullable(excludeBannerId).map(Collections::singletonList).orElse(null));
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "判断排序值是否可输入", notes = "true代表可输入，反之则不可")
    @GetMapping("/sort/available")
    public boolean sortAvailable(@ApiParam("排序值") @RequestParam int sort) {
        return readBannerService.sortAvailable(sort);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "列表分页查询")
    @GetMapping("/page")
    public PageInfo<SojournBannerVo> page(@ApiParam("关键词") @RequestParam(required = false) String keyWord,
                                          @ApiParam("展示状态(枚举值)") @RequestParam(required = false) Integer showType,
                                          @RequestParam int pageNo,
                                          @RequestParam int pageSize) {
        return readBannerService.page(keyWord, showType, pageNo, pageSize);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "根据id查询Banner详情")
    @GetMapping("/{bannerId}")
    public SojournBannerVo findVoById(@ApiParam(value = "Banner id") @PathVariable long bannerId) {
        return readBannerService.findVoById(bannerId);
    }
}
